﻿using System;
using System.Linq;
using Data.Entity;
using Service;
using System.Collections.Generic;

namespace ECommerce.Controls
{
    public partial class ProductDescription : BaseUserControl
    {
		private IProductService _ProductService;
		private IProductAdditionalPropertyService _ProductAdditionalPropertyService;

        public Product TheProductDataSource
        {
			get
			{
				if(CommonHelper.QueryStringInt("productid") > 0)
				{
					if(_ProductService == null)
					{
						_ProductService = new ProductService();
					}

					Product theProduct = _ProductService.GetProduct(CommonHelper.QueryStringInt("productid"));

					return theProduct;
				}
				else
				{
					return null;
				}
			}
		}

		public IList<ProductAdditionalProperty> TheAdditionalPropertyDataSource
		{
			get
			{
				if(CommonHelper.QueryStringInt("productid") > 0)
				{
					if(_ProductAdditionalPropertyService == null)
					{
						_ProductAdditionalPropertyService = new ProductAdditionalPropertyService();
					}

					var properties = _ProductAdditionalPropertyService.GetProductAdditionalProperties(CommonHelper.QueryStringInt("productid"));

					return properties;
				}
				else
				{
					return null;
				}
			}
		}

		protected void Page_Load(object sender, EventArgs e)
        {
        }

		protected void Page_PreRender(object sender, EventArgs e)
		{
			if (LiteralProdDescription != null)
			{
				LiteralProdDescription.Text = TheProductDataSource.FullDescription;
				//this.TextProdDescription.InnerText = TheProductDataSource.FullDescription;
				this.LabelProdName.Text = TheProductDataSource.Name;
				this.LabelProdCode.Text = TheProductDataSource.ProductCode != null ? TheProductDataSource.ProductCode : string.Empty;
				this.LabelProdSize.Text = TheProductDataSource.Size + "(" + TheProductDataSource.SizeUnit + ")";
				this.LabelProdWeight.Text = TheProductDataSource.Weight.Value.ToString() + "(" + TheProductDataSource.WeightUnit + ")";
				//this.LabelProdCategory.Text = GetCategoriesName();
				RepeaterCategoryLinks.DataSource = GetCategories();
				RepeaterCategoryLinks.DataBind();

				this.RepeaterProdAddtlProperty.DataSource = TheAdditionalPropertyDataSource;
				this.RepeaterProdAddtlProperty.DataBind();
			}

			this.LProductDescriptionTitle.Text = GetLocalResourceString("ProductDescription.Title");
			this.LProductDescriptionProdCodeLabel.Text = GetLocalResourceString("ProductDescription.ProductCodeLabel");
			this.LProductDescriptionNameLabel.Text = GetLocalResourceString("ProductDescription.NameLabel");
			this.LProductDescriptionCategoryLabel.Text = GetLocalResourceString("ProductDescription.CategoryLabel");
			this.LProductDescriptionSizeLabel.Text = GetLocalResourceString("ProductDescription.SizeLabel");
			this.LProductDescriptionWeightLabel.Text = GetLocalResourceString("ProductDescription.WeightLabel");
			this.LProductDescriptionDescriptionLabel.Text = GetLocalResourceString("ProductDescription.DescriptionLabel");
		}

		protected void RepeaterCategoryLinks_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if(e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Item || e.Item.ItemType == System.Web.UI.WebControls.ListItemType.AlternatingItem)
			{
				System.Web.UI.WebControls.HyperLink hl = (System.Web.UI.WebControls.HyperLink)e.Item.FindControl("HyperLinkCategoryLink");

				hl.Text = ((Data.Entity.Category)e.Item.DataItem).CategoryName;
				hl.NavigateUrl = "~/Category.aspx?categoryid=" + ((Data.Entity.Category)e.Item.DataItem).CategoryID;
			}
		}

        private IList<Data.Entity.Category> GetCategories()
        {
			if(_ProductService == null)
			{
				_ProductService = new ProductService();
			}

            return _ProductService.GetTheProductCategories(TheProductDataSource.ProductID);
        }
    }
}